package bb.lanxing.network;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.json.RouteBookModifyTime;

import rx.Observable;
import rx.schedulers.Schedulers;

public class RxJavaHttpClient {
    private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();

    public static void handleException(Throwable throwable) {
    }

    public static Observable<List<RouteBook>> requestLushuRecommendtionList(int type, int page, int limit, double lat,
                                                                            double lnt, long provinceId) {
        return Observable
                .create(new NetSubscribe(
                        MyHttpClient.requestLushuCollectionList(type, page, limit, lat, lnt, provinceId)))
                .subscribeOn(Schedulers.io()).doOnError(throwable -> {
                }).flatMap(s -> {
                    try {
                        List<RouteBook> arrayList = new ArrayList<>();
                        JSONObject obj = new JSONObject(s);
                        JSONArray jSONArray = obj.getJSONArray("rows");
                        for (int i = 0; i < jSONArray.length(); i++) {
                            arrayList.add(RouteBook.parseLushuByServer(jSONArray.getJSONObject(i), true));
                        }
                        return Observable.just(arrayList);
                    } catch (JSONException e) {
                        return Observable.error(e);
                    }
                });
    }

    public static Observable<Boolean> postLushuCollectionLike(long id) {
        return Observable.create(new NetSubscribe(MyHttpClient.postLushuCollectionLike(id)))
                .subscribeOn(Schedulers.io()).doOnError(throwable -> {
                }).flatMap(s -> Observable.just(Boolean.TRUE));
    }

    public static Observable<List<RouteBookModifyTime>> checkLushuForUpdate(List<RouteBook> list) {
        if (list == null || list.isEmpty()) {
            return Observable.just(null);
        }
        ArrayList<Long> arrayList = new ArrayList<>();
        for (RouteBook routeBook : list) {
            if (routeBook.getServerId() > 0) {
                arrayList.add(routeBook.getServerId());
            }
        }
        return Observable.create(new NetSubscribe(MyHttpClient.checkLushuForUpdate(arrayList)))
                .subscribeOn(Schedulers.io()).doOnError(RxJavaHttpClient::handleException).flatMap(s -> {
                    try {
                        return Observable
                                .just(RxJavaHttpClient.GSON.fromJson(new JSONObject(s).getJSONArray("data").toString(),
                                        new TypeToken<List<RouteBookModifyTime>>() {

                                        }.getType()));
                    } catch (JSONException e) {
                        return Observable.error(e);
                    }
                });
    }
}
